// pages/goodsDetail/goodsDetail.js
var core = require("../../utils/core.js");
var WxParse = require('../../libs/wxParse/wxParse.js');
import Toast from '../../components/dist/toast/toast';
import Notify from '../../components/dist/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodid: '',
    goodsInfo: '',
    timer: '',
    timeStr: '00:00:00',
    detiel: '',
    commetnData: [],
    commetnNum: '', //评价数量
    topMenuState: false,
    topMenuChooseState: 1,
    shangpingTop: 0,
    jilvTop: 0,
    xiangqingTop: 0,
    popup: false, //选择规格
    GoodNum: 1, //商品数量，默认一件
    buttonText: '确认购买', //按钮文字
    clickType: 'buynow', //点击按钮类型
    sum_total: '', //购物车数量

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var goodid = options.goodid;
    this.setData({
      goodid: goodid
    })
    console.log(goodid)
    this.getdata();

    // 获取相关高度
    wx.createSelectorQuery().select('#banner').boundingClientRect(function(rect) {
      // rect.top // 节点的上边界坐标
      console.log('购买记录：' + rect.top);
      that.setData({
        shangpingTop: rect.top
      })
    }).exec()
    wx.createSelectorQuery().select('#gmjl').boundingClientRect(function(rect) {
      // rect.top // 节点的上边界坐标
      console.log('购买记录：' + rect.top);
      that.setData({
        jilvTop: rect.top
      })
    }).exec()

    wx.createSelectorQuery().select('#goodsDetail').boundingClientRect(function(rect) {
      // rect.top // 节点的上边界坐标
      console.log('详情：' + rect.top);
      that.setData({
        xiangqingTop: rect.top
      })
    }).exec()





  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getdata: function() { //
    var that = this;
    core.get("groupapp/goods/group_goods_detail", { //商品信息
      id: that.data.goodid
    }, function(t) {
      console.log(t);
      that.ProcessingData(t.goods)


    })
    core.get("groupapp/goods/get_goods_comment", { //评论
      id: that.data.goodid,
      page: 1
    }, function(t) {
      if (t.error == 0 && t.comment.length > 0) {
        that.setData({
          commetnData: t.comment,
          commetnNum: t.count
        })
      } else {
        that.setData({
          commetnData: [],
          commetnNum: 0
        })
      }
    })

    that.getCartData();
  },
  getCartData: function() {
    var that = this;
    core.get("groupapp/goods/cart_statistics", { //获取购物车信息
    }, function(t) {
      console.log(t)
      that.setData({
        sum_total: t.sum_total
      })
    })
  },
  ProcessingData: function(data) { //处理数据
    var data = data;
    var that = this;
    var currentTime = new Date().getTime();
    var startTime = data.starttime * 1000;
    var endTime = data.endtime * 1000;
    if (startTime > currentTime) { //还没开始,计算的是 当前时间和开始时间
      console.log('还没开始');
      that.processingTime(startTime - currentTime);

    } else { //已经开始或者刚开始，计算的是结束时间和现在的差
      console.log('已经开始');
      that.processingTime(endTime - currentTime);
    }
    if (WxParse.wxParse('article', 'html', data.content, that, 5)) {
      that.setData({

        detiel: WxParse.wxParse('article', 'html', data.content, that, 5)

      })
    }
    data.all_total = data.total;
    that.setData({
      goodsInfo: data,


    })
  },
  processingTime: function(time) { //倒计时
    var that = this;
    var time = time;
    that.data.timer = setInterval(function() {
      if (time < 1000) {
        clearInterval(that.data.timer);
        that.getdata();

      } else {
        time -= 1000;
        that.formatDuring(time)
      }

    }, 1000)


  },
  formatDuring: function(mss) {
    var that = this;
    var days = parseInt(mss / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = parseInt((mss % (1000 * 60)) / 1000);

    var curhours = hours + days * 24;

    if (curhours < 10) {
      curhours = '0' + curhours;
    }
    if (minutes < 10) {
      minutes = '0' + minutes
    }
    if (seconds < 10) {
      seconds = '0' + seconds
    }
    var timeStr = curhours + ' : ' + minutes + ' : ' + seconds
    that.setData({
      timeStr: timeStr
    })
    return days + "天" + hours + "小时 " + minutes + "分钟 " + seconds + "秒 ";
  },
  GoIndex: function() { //回首页
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  Gocart: function() { //去购物车 
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  ClickIndex: function() {
    this.setData({

      popup: !1

    });
  },
  shangping: function() {
    var that = this;
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    that.setData({
      topMenuChooseState: 1
    })
  },
  jilv: function() {
    var that = this;
    wx.pageScrollTo({
      scrollTop: that.data.jilvTop - 50,
      duration: 300
    })
    that.setData({
      topMenuChooseState: 2
    })
  },
  xiangqing: function() {
    var that = this;
    wx.pageScrollTo({
      scrollTop: that.data.xiangqingTop - 50,
      duration: 300
    })
    that.setData({
      topMenuChooseState: 3
    })
  },
  reduceNum: function() {
    var GoodNum = this.data.GoodNum;
    if (GoodNum == 1) {
      Toast('最少购买一件');

    } else {
      this.setData({
        GoodNum: GoodNum - 1
      })
    }
  },
  addNum: function() {
    var that = this;
    var GoodNum = this.data.GoodNum;
    if (false) {

    } else {
      if (GoodNum == parseInt(that.data.goodsInfo.group_total)) {
        Toast('库存不足');
        return
      }
      this.setData({
        GoodNum: GoodNum + 1
      })
    }
  },

  clickTap: function(e) {
    var that = this;
    var type = e.target.dataset.type;
    if (type == 'cart') {
      that.setData({
        buttonText: '加入购物车'
      })
    } else if (type == 'buynow') {
      that.setData({
        buttonText: '确认购买'
      })
    }
    console.log(type)
    that.setData({
      popup: true,
      clickType: type, //点击按钮类型
    })
  },
  buy: function() { //确定购买
    var that = this;
    var goodNum = this.data.GoodNum; //商品数量
    if (that.data.clickType == 'buynow') {
      var goodsInfo = that.data.goodsInfo;
      goodsInfo.total = that.data.GoodNum;
      wx.setStorage({
        key: 'goodsInfo',
        data: goodsInfo,
        success: function() {
          wx.navigateTo({
            url: '/pages/confirmOrder/confirmOrder?type=detail',
          })
        }
      })


    } else if (that.data.clickType == 'cart') {
      console.log('加入购物车');
      core.get("groupapp/goods/add_cart", { //
        goodsid: that.data.goodid,
        total: goodNum
      }, function(t) {
        console.log(t);


        that.getCartData(); //重新获取购物车信息
      })




    }

    that.setData({
      popup: false,

    })


  },
  getAllComments: function() { //查看所有评论
    var that = this;
    wx.navigateTo({
      url: '/pages/allCommentList/allCommentList?id=' + that.data.goodid,
    })



  },
  current: function() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onPageScroll: function(res) {
    var that = this;
    let self = this;
    let top = res.scrollTop;
    if (top > 210) {
      that.setData({
        topMenuState: true
      })
    } else {
      that.setData({
        topMenuState: false
      })
    }
    // console.log(top)
    wx.createSelectorQuery().select('#topInfo').boundingClientRect(function(rect) {

      // rect.top // 节点的上边界坐标
      // console.log('顶部' + rect.top);

      if (rect.top > -90) {
        that.setData({
          topMenuChooseState: 1
        })
      } else if (rect.top < -90 && rect.top > -140) {
        that.setData({
          topMenuChooseState: 2
        })
      } else if (rect.top < -240 && rect.top > -397) {
        that.setData({
          topMenuChooseState: 3
        })
      }

      // console.log(that.data.topMenuChooseState)
    }).exec()
    // wx.createSelectorQuery().select('#gmjl').boundingClientRect(function (rect) {

    //   // rect.top // 节点的上边界坐标
    //   console.log('购买记录：'+rect.top);
    // }).exec()

    // wx.createSelectorQuery().select('#goodsDetail').boundingClientRect(function (rect) {

    //   // rect.top // 节点的上边界坐标
    //   // console.log('详情：'+rect.top);
    // }).exec()

  }
})